package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.OrderListBeanNew;
import com.joplus.housekeeper.core.BaseOrderType;
import com.joplus.housekeeper.core.OrderStatus;
import com.joplus.housekeeper.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by zisheng on 2017/9/18.
 */

public class OrderAdapter extends BaseAdapter {
    List<OrderListBeanNew> data;
    Context context;
    private final LayoutInflater layout;
    private final SimpleDateFormat simpleDateFormat;

    public OrderAdapter(Context context, List<OrderListBeanNew> data) {
        this.data = data;
        this.context = context;
        layout = LayoutInflater.from(context);
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm");
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    OrderHolder orderHolder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layout.inflate(R.layout.item_customer_order, null);
            orderHolder = new OrderHolder(convertView);
            convertView.setTag(orderHolder);
        } else {
            orderHolder = (OrderHolder) convertView.getTag();
        }
        OrderListBeanNew orderListBeanNew = data.get(position);
        BaseOrderType.OrderType orderType = BaseOrderType.getOrderType(orderListBeanNew);
        StringUtils.setText(orderHolder.type, orderType.name().equals("UnKnow") ? orderListBeanNew.getOrderType() : orderType.name());
        OrderStatus orderType1 = OrderStatus.getOrderType(orderListBeanNew.getOrderStatus());
        StringUtils.setText(orderHolder.tv_status, orderType1.getName());
        StringUtils.setText(orderHolder.tv_num, "订单编号:" + orderListBeanNew.getOrderNo());
        StringUtils.setText(orderHolder.tv_service, "处理人:" + orderListBeanNew.getOperName());
        StringUtils.setText(orderHolder.tv_need_pay, "应付:" + orderListBeanNew.getOrderMoney() + "元");
        StringUtils.setText(orderHolder.tv_pay, "实付:" + orderListBeanNew.getPaidMoney() + "元");

        StringUtils.setText(orderHolder.tv_num, "下单时间:" + simpleDateFormat.format(orderListBeanNew.getOrderDate()));


        return convertView;
    }


    public static class OrderHolder extends RecyclerView.ViewHolder {

        private TextView type;
        private TextView tv_status;
        private TextView tv_num;
        private TextView tv_service;
        private TextView tv_need_pay;
        private TextView tv_pay;
        private TextView tv_time;

        public OrderHolder(View itemView) {
            super(itemView);
            type = (TextView) itemView.findViewById(R.id.tv_type);
            tv_status = (TextView) itemView.findViewById(R.id.tv_status);
            tv_num = (TextView) itemView.findViewById(R.id.tv_num);
            tv_service = (TextView) itemView.findViewById(R.id.tv_service);
            tv_need_pay = (TextView) itemView.findViewById(R.id.tv_need_pay);
            tv_pay = (TextView) itemView.findViewById(R.id.tv_pay);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
        }


    }

}
